package com.xudu.tourbackend.controller.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xudu.tourbackend.common.DeleteBatchRequest;
import com.xudu.tourbackend.common.PageRequest;
import com.xudu.tourbackend.common.Result;
import com.xudu.tourbackend.common.globalEnum.ErrorCode;
import com.xudu.tourbackend.controller.ElementController;
import com.xudu.tourbackend.entity.Element;
import com.xudu.tourbackend.entity.request.AddElementRequest;
import com.xudu.tourbackend.entity.request.SearchElementRequest;
import com.xudu.tourbackend.entity.request.UpdateElementRequest;
import com.xudu.tourbackend.entity.vo.ScenicAreaVo;
import com.xudu.tourbackend.exception.ServiceException;
import com.xudu.tourbackend.service.IElementService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

/**
 * @className: ElementControllerImpl
 * @description: TODO
 * @author: xudu
 * @create: 2024-06-22
 */
@CrossOrigin(origins = "*")
@RestController
public class ElementControllerImpl implements ElementController {

    @Resource
    private IElementService elementService;

    @Override
    public Result addElement(AddElementRequest addElementRequest, HttpServletRequest request) {
        if (addElementRequest == null){
            throw new ServiceException(ErrorCode.NULL_ERROR);
        }
        int i = elementService.addElement(addElementRequest, request);
        return Result.success(i);
    }

    @Override
    public Result updateElement(UpdateElementRequest updateElementRequest, HttpServletRequest request) {
        if (updateElementRequest == null){
            throw new ServiceException(ErrorCode.NULL_ERROR);
        }
        Boolean b = elementService.updateElement(updateElementRequest, request);

        return Result.success(b);
    }

    @Override
    public Result deleteElement(@RequestBody DeleteBatchRequest deleteBatchRequest, HttpServletRequest request) {
        List<Integer> ids = deleteBatchRequest.getIds();
        if (ids.isEmpty()){
            throw new ServiceException(ErrorCode.NULL_ERROR);
        }
        int i = elementService.deleteElement(ids, request);
        return Result.success(i);
    }

    @Override
    public Result searchElement(SearchElementRequest searchElementRequest, HttpServletRequest request) {
        if (searchElementRequest == null){
            throw new ServiceException(ErrorCode.NULL_ERROR);
        }
        List<Element> elements = elementService.searchElement(searchElementRequest, request);

        //分页
        PageRequest pageRequest = new PageRequest();
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        PageInfo<Element> pageInfo = new PageInfo<>(elements);
        return Result.success(pageInfo);
    }

    @Override
    public List<Element> searchElementByName(String elementName, HttpServletRequest request) {
        return elementService.searchElement(elementName, request);
    }

    @Override
    public List<Element> searchElementByTag(int tagId) {
        if (tagId <= 0){
            return Collections.emptyList();
        }
        return elementService.searchElement(tagId);
    }
}
